import ajax from '../../lib/ajax'
import { Store, Module, ActionContext } from 'vuex'
import _ from 'lodash'

interface SessionState {
  application: any;
  user: any;
  token: any;
  authorStates: any;
  evaluatorStates: any;
}
class SessionStore implements Module<SessionState, any> {
  namespaced = true;
  state = {
    application: null,
    user: null,
    token: '',
    authorStates: JSON.parse(window.sessionStorage.getItem('authorStates') || '{}') || {},
    evaluatorStates: JSON.parse(window.sessionStorage.getItem('evaluatorStates') || '{}') || {}
  }

  actions = {
    async init(content: ActionContext<SessionState, any>) {
      //  ssss
    }
  }

  getters = {
    user(state: SessionState) {
      if (!_.size(state.user)) {
        let user = {}
        const userStr = window.sessionStorage.getItem('user') || ''
        const u = JSON.parse(userStr)
        if (u) {
          user = u
        }
        sessionStorage.setItem('user', JSON.stringify(user))
        state.user = user // this.$store.commit("setUser", user);
        return user
      }
      return state.user
    },
    token(state: any) {
      if (!_.size(state.token)) {
        state.token = window.sessionStorage.getItem('user') || ''
      }
      return state.token
    }
    // authorStates(state: any) {
    //   if (!_.size(state.authorStates)) {
    //     state.authorStates = window.sessionStorage.getItem('authorStates') || ''
    //   }
    //   return state.authorStates
    // },
    // evaluatorStates(state: any) {
    //   if (!_.size(state.evaluatorStates)) {
    //     state.evaluatorStates = window.sessionStorage.getItem('evaluatorStates') || ''
    //   }
    //   return state.evaluatorStates
    // }
  }

  mutations = {
    setUser(state: SessionState, user: any) {
      let _user = _.merge({}, state.user)
      if (!_.size(_user)) {
        _user = {}
      }
      _.each(user, function (value: any, key: any) {
        _user[key] = value
      })
      sessionStorage.setItem('user', JSON.stringify(_user))
      state.user = _user
    },
    setToken(state: SessionState, token: any) {
      console.log('token', token)
      sessionStorage.setItem('token', token)
      state.token = token
    },
    setAuthorStates(state: SessionState, states: any) {
      sessionStorage.setItem('authorStates', JSON.stringify(states))
      state.authorStates = states
    },
    setEvaluatorStates(state: SessionState, states: any) {
      sessionStorage.setItem('evaluatorStates', JSON.stringify(states))
      state.evaluatorStates = states
    },
    logout(state: SessionState) {
      sessionStorage.clear()
      state.user = {}
      state.token = null
    },
    login(state: SessionState) {
      let user = {}
      const userStr = window.sessionStorage.getItem('user') || ''
      const u = JSON.parse(userStr)
      if (u) {
        user = u
      }
      state.user = user // this.$store.commit("setUser", user);
      state.token = sessionStorage.getItem('token')
    }
  }
}
const session = new SessionStore()
export default session
